<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
<!--      <div>-->
<!--        &lt;!&ndash;使用 vue-qrcode 组件生成二维码 &ndash;&gt;-->
<!--        <vue-qrcode :value="qrValue" :size="200"></vue-qrcode>-->
<!--      </div>-->
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="5" :sm="8">
            <a-form-item label="所属区域">
              <j-select-depart placeholder="请选择" v-model="queryParam.areaId" />
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <a-form-item label="运行状态">
              <a-select v-model="queryParam.status" placeholder="请选择">
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option :value="1">运行</a-select-option>
                <a-select-option :value="2">停机</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <a-form-model-item label="安装时间起">
              <j-date
                placeholder="请选择安装时间起"
                v-model="queryParam.installTime_begin"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <a-form-model-item label="安装时间止">
              <j-date
                placeholder="请选择安装时间止"
                v-model="queryParam.installTime_end"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <a-form-item label="终端名称">
              <a-input placeholder="模糊查询" v-model="terminalName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <a-form-item label="终端编号">
              <a-input placeholder="请输入" v-model="queryParam.terminalNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="customerSearchReset" icon="reload" style="margin-left: 8px"
                >重置</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('农污终端表')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <!-- <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <!-- :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" -->
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="qrCodeImg" slot-scope="qrCodeImg,record" >
<!--          <span>{{record.terminalName}}</span>-->
<!--            <a-qrcode value="https://img1.baidu.com/" />-->
<!--          <qrcode value="https://img1.baidu.com/" :size="200" color="" type="image"></qrcode>-->
          <img :src="qrCodeImg" alt="" height="30px" @click="changeFn(qrCodeImg,record.terminalName)"/>
          <a-modal :title="terName"
                   maskClosable
                   :destroyOnClose="false"
                   :visible="visible"  :closable="false"
                   :maskStyle="{background:'rgba(0,0,0,.1)'}"
                   :bodyStyle="{ height: '420px', overflowY: 'auto' }"
                   centered>
            <div style="text-align: center"><img :src="urlImage" alt="" height="84%" width="74%"/></div>
            <div class="print" @click="loadCode()">下载打印</div>
            <template slot="footer">
              <a-button type="primary"  @click="confirmMessage">确认</a-button>
            </template>
          </a-modal>
        </template>

        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="record.id"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.terminalId)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <terminal-modal ref="modalForm" @ok="modalFormOk"></terminal-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TerminalModal from './modules/Terminal/TerminalModal'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
// import QRCode from 'qrcode';
export default {
  name: 'TerminalList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    TerminalModal,
  },
  data() {
    return {
      visible:false,
      urlImage:'',
      terName:'',
      description: '农污终端表管理页面',
      terminalName: '',
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: (t, r, index) => {
            const { pageSize, current } = this.ipagination
            return (current - 1) * pageSize + parseInt(index) + 1
          },
        },
        {
          title: '终端名称',
          align: 'center',
          dataIndex: 'terminalName',
        },
        {
          title: '终端编号',
          align: 'center',
          dataIndex: 'terminalId',
        },
        {
          title: '经度',
          align: 'center',
          dataIndex: 'longitude',
        },
        {
          title: '纬度',
          align: 'center',
          dataIndex: 'latitude',
        },
        {
          title: '占地面积（㎡）',
          align: 'center',
          dataIndex: 'floorSpace',
        },
        {
          title: '材质',
          align: 'center',
          dataIndex: 'material',
          customRender: (text) => {
            return filterDictTextByCache('nw_terminal_material', text)
          },
        },
        {
          title: '运行状态',
          align: 'center',
          dataIndex: 'status',
          customRender: (text) => {
            return text == 1 ? '运行' : text == 2 ? '停机' : text
          },
        },
        {
          title: '安装时间',
          align: 'center',
          dataIndex: 'installTime',
        },
        {
          title: '厂家',
          align: 'center',
          dataIndex: 'factory',
        },
        {
          title: '联系方式',
          align: 'center',
          dataIndex: 'factoryTel',
        },
        {
          title: '二维码',
          align: 'center',
          dataIndex: 'qrCode',
          scopedSlots: { customRender: 'qrCodeImg' },

        },
        // {
        //   title: '负责人',
        //   align: 'center',
        //   dataIndex: 'manager',
        // },
        // {
        //   title: '联系电话',
        //   align: 'center',
        //   dataIndex: 'managerTel',
        // },
        // {
        //   title: '品牌',
        //   align: 'center',
        //   dataIndex: 'brand',
        // },
        // {
        //   title: '位置',
        //   align: 'center',
        //   dataIndex: 'position',
        // },
        // {
        //   title: '使用寿命',
        //   align: 'center',
        //   dataIndex: 'serviceLife',
        // },
        // {
        //   title: '日处理能力',
        //   align: 'center',
        //   dataIndex: 'dailyProcessing',
        // },
        // {
        //   title: '排放标准',
        //   align: 'center',
        //   dataIndex: 'emissionStandards',,
        // customRender: (text) => {
        //   return filterDictTextByCache('nw_terminal_emission_standards', text)
        // },
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/terminal/terminal/list',
        delete: '/terminal/terminal/delete',
        deleteBatch: '/terminal/terminal/deleteBatch',
        exportXlsUrl: '/terminal/terminal/exportXls',
        importExcelUrl: 'terminal/terminal/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()

  },
  mounted() {
    // this.getQrcodeImage(this.qrbase64)

  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  watch: {
    terminalName(terminalName) {
      if (terminalName) {
        this.queryParam.terminalName = `*${terminalName}*`
      } else {
        this.queryParam.terminalName = ''
      }
    },
  },
  methods: {
    confirmMessage(){
    this.visible=false
    },
    changeFn(url,title){
          this.terName=title
          this.urlImage=url
         this.visible=true
           },
    loadCode() {
       let image = new Image();
    let a = document.createElement("a"); // 生成一个a元素
    let event = new MouseEvent("click"); // 创建一个单击事件
    a.download = this.terName || "终端图片"; // 设置图片名称
    a.href = this.urlImage; // 将生成的URL设置为a.href属性
    a.dispatchEvent(event); // 触发a的单击事件
     image.src = this.urlImage;
               },
    // getQrcodeImage(urlImg){
    //   console.log(urlImg,'ashnoifhioashfhbn')
    //   let qrcodeImage
    //     QRCode.toDataURL(urlImg)
    //     .then(url => {
    //       console.log(url)
    //      qrcodeImage=url
    //       return qrcodeImage
    //     })
    //     .catch(err => {
    //       console.error(err)
    //     })
    //   // return qrcodeImage
    //   console.log(qrcodeImage,'333')
    // },
    // imgShow(qrcodeImage,record) {
    //   const image = new Image()
    //   image.src = qrcodeImage
    //   image.onload = () => {
    //     //创建弹出层
    //     const previewContatiner = document.createElement('div')
    //     previewContatiner.style.position = 'fixed'
    //     previewContatiner.style.top = 0
    //     previewContatiner.style.bottom = 0
    //     previewContatiner.style.left = 0
    //     previewContatiner.style.right = 0
    //     previewContatiner.style.zIndex = 9999
    //     previewContatiner.style.backgroundColor = 'rgba(0,0,0,0.8)'
    //     previewContatiner.style.display = 'flex'
    //     previewContatiner.style.justifyContent = 'center'
    //     previewContatiner.style.alignItems = 'center'
    //     document.body.appendChild(previewContatiner)
    //     //在弹出层增加图片
    //     const previewImage = document.createElement('img')
    //     previewImage.src = qrcodeImage
    //     previewImage.style.maxWidth = '100%'
    //     previewImage.style.maxHeight = '100%'
    //     previewImage.style.zIndex = 9999
    //     previewContatiner.appendChild(previewImage)
    //     //增加终端名字
    //     const name = document.createElement('h1')
    //     name.innerHTML=record.terminalName
    //     previewContatiner.appendChild(name)
    //     //点击弹出层，关闭预览
    //     previewContatiner.addEventListener('click', () => {
    //       document.body.removeChild(previewContatiner)
    //     })
    //   }
    //   image.onerror = function () {
    //     console.log('图片加载失败')
    //   }
    // },
    customerSearchReset() {
      this.terminalName = ''
      this.searchReset()
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'terminalName', text: '终端名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'terminalNo', text: '终端编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'manager', text: '负责人', dictCode: '' })
      fieldList.push({ type: 'string', value: 'managerTel', text: '联系电话', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'longitude', text: '经度', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'latitude', text: '纬度', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'floorSpace', text: '占地面积', dictCode: '' })
      fieldList.push({ type: 'int', value: 'material', text: '材质', dictCode: '' })
      fieldList.push({ type: 'string', value: 'factory', text: '厂家', dictCode: '' })
      fieldList.push({ type: 'string', value: 'factoryTel', text: '厂家电话', dictCode: '' })
      fieldList.push({ type: 'string', value: 'brand', text: '品牌', dictCode: '' })
      fieldList.push({ type: 'string', value: 'position', text: '位置', dictCode: '' })
      fieldList.push({ type: 'int', value: 'serviceLife', text: '使用寿命', dictCode: '' })
      fieldList.push({ type: 'string', value: 'dailyProcessing', text: '日处理能力', dictCode: '' })
      fieldList.push({ type: 'int', value: 'emissionStandards', text: '排放标准', dictCode: '' })
      fieldList.push({ type: 'datetime', value: 'installTime', text: '安装时间' })
      fieldList.push({ type: 'int', value: 'status', text: '终端状态', dictCode: '' })
      this.superFieldList = fieldList
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.print{
  width: 70px;
  height: 28px;
  background-color: #cccccc;
  line-height: 28px;
  text-align: center;
  font-weight: bold;
}
</style>
